import { Component, OnInit } from '@angular/core';
import { NavigationExtras, Router } from '@angular/router';
import { ConfirmationService, MessageService } from 'primeng/api';
import { PostService } from 'src/app/service/post.service';

@Component({
    selector: 'app-post-manage',
    templateUrl: './post-manage.component.html',
    styleUrls: ['./post-manage.component.scss'],
    providers: [ConfirmationService, MessageService],
})
export class PostManageComponent implements OnInit {
    tableData: any;
    displayPost = false;
    currentItem: any;
    modelTitle = '';
    mode = '';
    form = {
        name: '',
        desc: '',
    };
    constructor(
        private postService: PostService,
        private confirmationService: ConfirmationService,
        private messageService: MessageService,
        private router: Router
    ) {}

    ngOnInit(): void {
        this.fetchPost();
    }
    async fetchPost() {
        const data = (await this.postService.fetchPostList()) || [];
        console.log('data', data);
        this.tableData = data;
    }
    async addPost() {
        if (!this.form.name || !this.form.desc) {
            return;
        }
        const res = await this.postService.addPost({
            name: this.form.name,
            desc: this.form.desc,
        });
        if (res) {
            this.messageService.add({
                severity: 'success',
                summary: '确认',
                detail: '已经完成操作',
            });
            this.fetchPost();
        }
    }
    async editPost() {
        if (!this.form.name || !this.form.desc) {
            return;
        }
        const res = await this.postService.editPostByConfig({
            roleId: this.currentItem.roleId,
            roleName: this.form.name,
            description: this.form.desc,
        });
        if (res) {
            this.messageService.add({
                severity: 'success',
                summary: '确认',
                detail: '已经完成操作',
            });
            this.fetchPost();
        }
    }
    submit() {
        if (this.mode == 'add') {
            this.addPost();
        } else if (this.mode == 'edit') {
            this.editPost();
        }
        this.displayPost = false;
    }
    async deletePost({ roleId: id }) {
        this.confirmationService.confirm({
            message: '请问你确认删除吗 ?',
            header: '警告',
            icon: 'pi pi-exclamation-triangle',
            accept: async () => {
                const res = await this.postService.deletePost({ id });
                if (res) {
                    this.messageService.add({
                        severity: 'info',
                        summary: '确认',
                        detail: '你已经删除',
                    });
                    this.fetchPost();
                }
            },
        });
    }
    openDialog(a, item?) {
        this.displayPost = true;
        if (a) {
            this.modelTitle = '新增岗位';
            this.mode = 'add';
            this.form.name = '';
            this.form.desc = '';
        } else {
            this.currentItem = item;
            this.modelTitle = '修改岗位';
            this.mode = 'edit';
            this.form.name = item.roleName;
            this.form.desc = item.description;
        }
    }
    goToAccountToPost({ roleId: id }) {
        let navigationExtras: NavigationExtras = {
            queryParams: { id: id },
        };
        this.router.navigate(['/pages/manage/account2post'], navigationExtras);
    }
    goToAcceptManage({ roleId: id }) {
        let navigationExtras: NavigationExtras = {
            queryParams: { id: id },
        };
        this.router.navigate(['/pages/manage/accept'], navigationExtras);
    }
    goToPermission({ roleId: id }) {
      let navigationExtras: NavigationExtras = {
          queryParams: { id: id },
      };
      this.router.navigate(['/pages/manage/permission'], navigationExtras);
  }
}
